package com.example.system.service.impl;

import com.example.core.enums.ResultCode;
import com.example.scurity.exception.ServiceException;
import com.example.system.domain.user.DTO.UserQueryDTO;
import com.example.system.domain.user.DTO.UserUpdateDTO;
import com.example.system.domain.user.User;
import com.example.system.domain.user.VO.UserVo;
import com.example.system.mapper.UserMapper;
import com.example.system.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserVo> list(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getPageNum(),
                userQueryDTO.getPageSize());
        return userMapper.getUserList(userQueryDTO);
    }

    @Override
    public int updateStatus(UserUpdateDTO userUpdateDTO) {
        User user = userMapper.selectById(userUpdateDTO.getUserId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(userUpdateDTO.getStatus());

        return userMapper.updateById(user);
    }
}
